package com.td.module.system.controller.admin.growth;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.system.controller.admin.growth.vo.type.*;
import com.td.module.system.convert.growth.GrowthModuleTypeConvert;
import com.td.module.system.dal.dataobject.growth.GrowthModuleTypeAndListDO;
import com.td.module.system.dal.dataobject.growth.GrowthModuleTypeDO;
import com.td.module.system.service.growth.GrowthModuleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 生长模型字典")
@RestController
@RequestMapping("/growth/module-type")
@Validated
public class GrowthModuleTypeController {

    @Resource
    private GrowthModuleTypeService moduleTypeService;

    @PostMapping("/create")
    @ApiOperation("创建生长模型字典")
    @PreAuthorize("@ss.hasPermission('growth:module-type:create')")
    public CommonResult<Long> createModuleType(@Valid @RequestBody GrowthModuleTypeCreateReqVO createReqVO) {
        return success(moduleTypeService.createModuleType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新生长模型字典")
    @PreAuthorize("@ss.hasPermission('growth:module-type:update')")
    public CommonResult<Boolean> updateModuleType(@Valid @RequestBody GrowthModuleTypeUpdateReqVO updateReqVO) {
        moduleTypeService.updateModuleType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除生长模型字典")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('growth:module-type:delete')")
    public CommonResult<Boolean> deleteModuleType(@RequestParam("id") Long id) {
        moduleTypeService.deleteModuleType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得生长模型字典")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<GrowthModuleTypeRespVO> getModuleType(@RequestParam("id") Long id) {
        GrowthModuleTypeDO moduleType = moduleTypeService.getModuleType(id);
        return success(GrowthModuleTypeConvert.INSTANCE.convert(moduleType));
    }

    @GetMapping("/list")
    @ApiOperation("获得生长模型字典列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<GrowthModuleTypeRespVO>> getModuleTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<GrowthModuleTypeDO> list = moduleTypeService.getModuleTypeList(ids);
        return success(GrowthModuleTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得生长模型字典分页")
    public CommonResult<PageResult<GrowthModuleTypeRespVO>> getModuleTypePage(@Valid GrowthModuleTypePageReqVO pageVO) {
        PageResult<GrowthModuleTypeDO> pageResult = moduleTypeService.getModuleTypePage(pageVO);
        return success(GrowthModuleTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出生长模型字典 Excel")
    @PreAuthorize("@ss.hasPermission('growth:module-type:export')")
    @OperateLog(type = EXPORT)
    public void exportModuleTypeExcel(@Valid GrowthModuleTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GrowthModuleTypeDO> list = moduleTypeService.getModuleTypeList(exportReqVO);
        // 导出 Excel
        List<GrowthModuleTypeExcelVO> datas = GrowthModuleTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生长模型字典.xls", "数据", GrowthModuleTypeExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获得全部生长模型列表", notes = "包括开启 + 禁用的生长模型，主要用于前端的下拉选项")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<GrowthModuleTypeSimpleRespVO>> listSimpleGrowthTypes() {
        List<GrowthModuleTypeDO> list = moduleTypeService.getModuleTypeList();
        return success(GrowthModuleTypeConvert.INSTANCE.convertList03(list));
    }
    @GetMapping("/list-all-column")
    @ApiOperation(value = "获得全部生长模型列表和模型列列表")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<GrowthModuleTypeAndColumnRespVO>> getModuleTypeAndColumnList() {
        List<GrowthModuleTypeAndListDO> list = moduleTypeService.getModuleTypeAndColumnList();
        return success(GrowthModuleTypeConvert.INSTANCE.convertList04(list));
    }
    @GetMapping("/get-all-column")
    @ApiOperation(value = "获得单个生长模型列表和模型列列")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<GrowthModuleTypeAndColumnRespVO> getOneGrowthModuleTypeAndList(@RequestParam("id") Long id) {
        GrowthModuleTypeAndListDO growth = moduleTypeService.getOneGrowthModuleTypeAndList(id);
        return success(GrowthModuleTypeConvert.INSTANCE.convertColumn(growth));
    }
}
